package com.xscha.tableforge.controller;

import com.xscha.tableforge.model.dto.TableMetaDto;
import com.xscha.tableforge.model.entity.TableMeta;
import com.xscha.tableforge.model.vo.TableMetaVo;
import com.xscha.tableforge.model.vo.PageReq;
import com.xscha.tableforge.model.vo.PageResult;
import com.xscha.soyue.common.core.domain.R;
import com.xscha.tableforge.model.vo.ViewMetaVo;
import com.xscha.tableforge.service.TableMetaService;
import com.xscha.soyue.common.core.utils.BeanMapperUtils;
import com.xscha.tableforge.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/tables")
public class TableMetaController {

    private final TableMetaService tableMetaService;

    @Autowired
    public TableMetaController(TableMetaService tableMetaService) {
        this.tableMetaService = tableMetaService;
    }

    @GetMapping("/app/{appId}")
    public R<List<TableMetaVo>> getTablesByAppId(@PathVariable String appId) {
        List<TableMeta> tables = tableMetaService.findAllByAppId(appId);
        return R.ok(BeanMapperUtils.mapAsList(tables, TableMetaVo.class));
    }
    
    @PostMapping("/app/{appId}/page")
    public R<PageResult<TableMetaVo>> getTablesByAppIdPage(
            @PathVariable String appId,
            @RequestBody PageReq pageRequest) {
        PageResult<TableMeta> result = tableMetaService.findPageByAppId(appId, pageRequest);
		List<TableMetaVo> tableMetaVos = BeanMapperUtils.mapAsList(result.getRecords(), TableMetaVo.class);
		return R.ok(PageResult.of(tableMetaVos, result.getTotal(), result.getPageNum(), result.getPageSize()));
    }

    @GetMapping("/{id}")
    public R<TableMetaVo> getTableById(@PathVariable String id) {
        try {
            TableMetaDto tableMetaDTO = tableMetaService.getTableMetaWithFields(id);
            return R.ok(BeanMapperUtils.map(tableMetaDTO, TableMetaVo.class));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    @PostMapping
    public R<TableMetaVo> createTable(@RequestBody TableMetaDto tableMetaDto) {
        try {
            TableMetaDto createdTable = tableMetaService.createTableMeta(tableMetaDto, SecurityUtils.getUserId());
            return R.ok(BeanMapperUtils.map(createdTable, TableMetaVo.class));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    @PutMapping("/{id}")
    public R<TableMetaVo> updateTable(@PathVariable String id, 
                                     @RequestBody TableMetaDto tableMetaDto) {
        if (!id.equals(tableMetaDto.getId())) {
            return R.fail("ID不匹配");
        }
        
        try {
            TableMeta updatedTable = tableMetaService.updateTableMeta(id, tableMetaDto, SecurityUtils.getUserId());
            return R.ok(BeanMapperUtils.map(updatedTable, TableMetaVo.class));
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public R<Void> deleteTable(@PathVariable String id) {
        try {
            tableMetaService.deleteTableMeta(id, SecurityUtils.getUserId());
            return R.ok();
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
    }
}